Springboot 实现在线预览,Word文档 doc、xlsx、pdf、txt 您所在的位置:网站首页 txt转pdf 在线 Springboot 实现在线预览,Word文档 doc、xlsx、pdf、txt

Springboot 实现在线预览,Word文档 doc、xlsx、pdf、txt

2023-06-21 19:48| 来源: 网络整理| 查看: 265

前言 PDF、TXT 只要资源可访问,根本就不需要进行任何处理,直接访问查看就完事了。

也是因为这个PDF可以直接查看(现在浏览器基本支持了),那么我们实现Word文档在线预览,其实也是 把WORD文档 复制一份生成一份供预览的 PDF文件而已。

先看看效果:

 

正文

这篇实例,实现在线预览WORD文档,分两步:

一. 安装OpenOffice

二.写点小代码

一. 安装OpenOffice 不要看到安装东西就觉得麻烦,因为这个安装不需要做任何配置,你只需要下载,选择安装地址,一直下一步 即可。

那么下载地址,我也给你了(Windows版, 线上linux服务器安装的话随便搜一下 Linux 安装 openoffice 就可以):

百度网盘链接:百度网盘 请输入提取码icon-default.png?t=LA46https://pan.baidu.com/s/17y5O7IqkB0YxA6tWdjJNvA

提取码:

7u23

安装完之后,你只需要知道你安装在哪里了即可,就像我:

我的安装地址是(因为一会项目代码需要用到): 

C:\Program Files (x86)\OpenOffice 4 二.写点小代码

首先,pom.xml文件加上核心的依赖(我的springboot版本用的 2.1.4.RELEASE):

org.springframework.boot spring-boot-starter-web org.jodconverter jodconverter-core 4.2.2 org.jodconverter jodconverter-spring-boot-starter 4.2.2 org.jodconverter jodconverter-local 4.2.2 commons-io commons-io 2.6

接着是 application.yml文件:

jodconverter: local: enabled: true office-home: C:\Program Files (x86)\OpenOffice 4 max-tasks-per-process: 10 port-numbers: 8100

然后就是实现代码,非常简短,我们直接写在接口里,方便我测试(你们自己可以提出来):

 新建一个 TestController.java:

import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.UUID; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.jodconverter.DocumentConverter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TestController { @Autowired private DocumentConverter converter; //用于转换 @ResponseBody @RequestMapping("testPreview") public void toPdfFile(HttpServletResponse response) { File file = new File("D:\\testMyDoc\\doc\\testJc.docx");//需要转换的文件 try { File newFile = new File("D:/testMyDoc");//转换之后文件生成的地址 if (!newFile.exists()) { newFile.mkdirs(); } String savePath="D:/testMyDoc/"; //pdf文件生成保存的路径 String fileName="JCccc"+UUID.randomUUID().toString().replaceAll("-","").substring(0,6); String fileType=".pdf"; //pdf文件后缀 String newFileMix=savePath+fileName+fileType; //将这三个拼接起来,就是我们最后生成文件保存的完整访问路径了 //文件转化 converter.convert(file).to(new File(newFileMix)).execute(); //使用response,将pdf文件以流的方式发送的前端浏览器上 ServletOutputStream outputStream = response.getOutputStream(); InputStream in = new FileInputStream(new File(newFileMix));// 读取文件 int i = IOUtils.copy(in, outputStream); // copy流数据,i为字节数 in.close(); outputStream.close(); System.out.println("流已关闭,可预览,该文件字节大小:"+i); } catch (Exception e) { e.printStackTrace(); } } }

代码简单分析:

好了,不多说,到这里已经实现了。

 

那么我们简单测试一下,运行项目:

这是代码里面写死的需要预览的doc文件:

然后我们在浏览器访问接口  http://localhost:8089/testPreview  ,可以看到预览没问题:

 

 

看一眼,我们生成的用于预览pdf文件:

控制台:



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有